package com.chaoxuan.uebt.utils;

import android.accounts.NetworkErrorException;
import android.util.Log;
import android.widget.Toast;

import com.chaoxuan.uebt.App;
import com.chaoxuan.uebt.model.Result;

import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import rx.Observer;
import rx.Subscriber;

/**
 * Created by Administrator on 2019/5/23.
 */
public abstract class BaseSubscriber<T> extends Subscriber<Result<T>> {

    @Override
    public void onNext(Result<T> result) {
        if (result != null && result.isError()) {
            if(!"用户暂未填写".equals(result.getMsg())){
                Toast.makeText(App.getInstance(), result.getMsg(), Toast.LENGTH_LONG).show();
                return;
            }
        }
        onSuccess(result.getData());
    }

    @Override
    public void onError(Throwable e) {
        onCompleted();
        Log.d("======",e.getMessage());
        try {
            if (e instanceof ConnectException || e instanceof TimeoutException || e instanceof NetworkErrorException || e instanceof UnknownHostException) {
                Toast.makeText(App.getInstance(), "net error", Toast.LENGTH_LONG);
            } else {
                Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_LONG);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    @Override
    public void onCompleted() {
        Log.d("----","onCompleted");
    }

    public abstract void onSuccess(T t);
}
